package io.renren.modules.user.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.modules.driver.service.SjDriverService;
import io.renren.modules.user.entity.UserEntity;
import io.renren.modules.user.service.WebUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 用户
 * 
 * @author zhangheng
 * @email zh_123shmily@163.com
 * @date 2017-09-15 09:50:28
 */
@RestController
@RequestMapping("user")
public class UserController {
	@Autowired
	private WebUserService userService;

	@Autowired
	private SjDriverService sjDriverService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("user:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<UserEntity> userList = userService.queryList(query);
		int total = userService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(userList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{userId}")
	@RequiresPermissions("user:info")
	public R info(@PathVariable("userId") Long userId){
		UserEntity user = userService.queryObject(userId);
		
		return R.ok().put("user", user);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("user:save")
	public R save(@RequestBody UserEntity user){
		String passWord = new Sha256Hash("123456").toHex();
		user.setPassword(passWord);
		userService.save(user);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("user:update")
	public R update(@RequestBody UserEntity user){
		String passWord = new Sha256Hash(user.getPassword()).toHex();
		user.setPassword(passWord);
		userService.update(user);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("user:delete")
	public R delete(@RequestBody Long[] userIds){
		userService.deleteBatch(userIds);
		
		return R.ok();
	}

	/**
	 * 将普通用户设置为司机
	 */
	@RequestMapping("/becomeDriver")
	@RequiresPermissions("user:becomeDriver")
	public R becomeDriver(@RequestBody Long[] userIds){
		userService.becomeDriver(userIds);

		return R.ok();
	}



	
}
